import Vue from 'vue'
import Vuex from 'vuex'
import {getCartNumber, getSiteConfig, getUserInfo} from "../api/apis";

Vue.use(Vuex)

const state = {
    userInfo: null,
    cartNumber: '',
    backendUrl: false,
    siteTitle: '',
    qq: '',
    service_qq: '',
    tel: '',
    email: '',
    address: '',
    copyright: '',
    publicqrcode: '',
    productIntro: ''
}


export default new Vuex.Store({
    state,
    mutations: {
        userNo() {
            state.userInfo = null
        },
        GetUserInfo() {
            getUserInfo().then(res => {
                state.userInfo = res.data
            })
        },
        changeCartNumber(state) {
            getCartNumber().then(result => {
                if (parseInt(result.data) > 0) {
                    state.cartNumber = parseInt(result.data);
                } else {
                    state.cartNumber = "";
                }
            });
        },
        getSiteConfig(state) {
            getSiteConfig().then(result => {
                if (result.status == 'SUCCESS') {
                    state.backendUrl = result.data.backend_url;
                    state.qq = result.data.qq;
                    state.siteTitle = result.data.title;
                    state.service_qq = result.data.service_qq;
                    state.tel = result.data.tel;
                    state.email = result.data.email;
                    state.address = result.data.address;
                    state.copyright = result.data.copy_right;
                    state.publicqrcode = result.data.public_qrcode;
                    state.productIntro = result.data.product_intro;
                } else {
                    state.backendUrl = false;
                    state.qq = false;
                    state.siteTitle = "协科光电";
                    state.service_qq = false;
                    state.tel = false;
                    state.email = false;
                    state.address = false;
                    state.copyright = "蜀ICP备19004229号-1";
                    state.publicqrcode = false;
                    state.productIntro = false;
                }
            });
        }
    }
})

